package com.wind.cqboot.plugin;

import org.springframework.stereotype.Component;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.wind.cqboot.utils.HttpUtil;

import cn.hutool.core.util.StrUtil;
import net.lz1998.cq.event.message.CQGroupMessageEvent;
import net.lz1998.cq.robot.CQPlugin;
import net.lz1998.cq.robot.CoolQ;

/**
 * <p>File：InterestingPlugin.java</p>
 * <p>Title: 有趣接口合集</p>
 * <p>Description: 包含渣男语录、一言、毒鸡汤、舔狗日记</p>
 * <p>Copyright: Copyright (c) 2020 2020年3月26日 下午3:12:40</p>
 * <p>Company: zmrit.com </p>
 * @author zmr
 * @version 1.0
 */
@Component
public class InterestingPlugin extends CQPlugin
{
    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event)
    {
        String msg = event.getMessage();
        Long groupId = event.getGroupId();
        String content = "";
        if (msg.equals("渣男语录"))
        {
            // WebSite/{n} n是整数，表示返回的条数，不写返回一条
            content = HttpUtil.sendGet("https://api.lovelive.tools/api/SweetNothings/WebSite");
        }
        else if (msg.equals("毒鸡汤"))
        {
            content = buildDujitang();
        }
        else if (msg.equals("一言"))
        {
            content = buildYiyan();
        }
        else if (msg.equals("舔狗日记"))
        {
            content = buildTiangou();
        }
        if (StrUtil.isNotBlank(content))
        {
            cq.sendGroupMsg(groupId, content, false);
            return MESSAGE_BLOCK;
        }
        // 继续执行下一个插件
        return MESSAGE_IGNORE;
    }

    // 一言
    private static String buildYiyan()
    {
        String result = HttpUtil.sendGet("https://api.daidr.me/apis/hitokoto.json");
        if (StrUtil.isNotBlank(result))
        {
            JsonObject data = new JsonParser().parse(result).getAsJsonObject();
            StringBuffer sb = new StringBuffer();
            String text = "";
            sb.append("一言\r\n");
            sb.append(data.get("content").getAsString()).append("\r\n");
            sb.append("--来自：").append(data.get("from").getAsString());
            text = sb.toString();
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }

    // 毒鸡汤
    private static String buildDujitang()
    {
        String result = HttpUtil.sendGet("https://api.daidr.me/apis/poisonous.json");
        if (StrUtil.isNotBlank(result))
        {
            JsonObject data = new JsonParser().parse(result).getAsJsonObject();
            return data.get("content").getAsString();
        }
        return "机器人暂时失联了，请稍后再试";
    }

    // 舔狗日记
    private static String buildTiangou()
    {
        String result = HttpUtil.sendGet("https://www.somekey.cn/tiangou/random.php");
        if (StrUtil.isNotBlank(result))
        {
            JsonObject json = new JsonParser().parse(result).getAsJsonObject();
            StringBuffer sb = new StringBuffer();
            String text = "";
            if (json.get("code").getAsInt() == 1)
            {
                JsonObject data = json.get("data").getAsJsonObject();
                sb.append("舔狗日记\r\n");
                sb.append("日期：").append(data.get("date").getAsString()).append("\r\n");
                sb.append("天气：").append(data.get("weather").getAsString()).append("\r\n");
                sb.append(data.get("content").getAsString());
                text = sb.toString();
            }
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }
}
